su
<template> 
  <div>
    <el-upload
      :action="action"
      :data="dataObj"
      :multiple="false"
      :show-file-list="showFileList"
      list-type="text"
      :file-list="fileList"
      :before-upload="beforeUpload"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-preview="handlePreview"
      :on-progress="uploadAudioProcess"
    >
      <el-button type="primary">点击添加</el-button>
      <div class="el-upload__tip">最多可以上传1个音频，最大上传1G，推荐格式mp3</div>
      <el-progress v-if="audioFlag == true" :percentage="audioUploadPercent" style="margin-top:30px;"></el-progress>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" :before-close="handleClose">
      <div style="display: flex;justify-content: center;align-content: center">
        <audio id="audio" controls="controls" v-bind:src="fileList[0].url"></audio>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {policy} from '@/api/oss'
  import utils from '../../utils/utils'

  export default {
    name: 'singleUpload',
    props: {
      value: String
    },
    computed: {
      imageUrl() {
        return this.value;
      },
      imageName() {
        if (this.value != null && this.value !== '') {
          return this.value.substr(this.value.lastIndexOf("/") + 1);
        } else {
          return null;
        }
      },
      fileList() {
        return [{
          name: this.imageName,
          url: this.imageUrl
        }]
      },
      showFileList: {
        get: function () {
          return this.value !== null && this.value !== '' && this.value !== undefined;
        },
        set: function (newValue) {
        }
      }
    },
    data() {
      return {
        dataObj: {
          policy: '',
          signature: '',
          key: '',
          ossaccessKeyId: '',
          dir: '',
          host: ''
        },
        action: '',
        dialogVisible: false,
        audioFlag: false,
        audioUploadPercent: 0,
        uuid: ''
      };
    },
    methods: {
      emitInput(val) {
        this.$emit('input', val)
      },
      handleRemove(file, fileList) {
        if (file && file.status === "success") {
          this.emitInput('');
        }
      },
      handlePreview(file) {
        this.dialogVisible = true;
      },
      beforeUpload(file) {
        let _self = this;
        _self.uuid = utils.getUUID()
        console.log(this.fileList);
        console.log(this.showFileList)
        if (!utils.isNull(this.fileList[0].name) && !utils.isNull(this.fileList[0].url)) {
          this.handleRemove()
        }
        console.log(file)
        if (file.type.indexOf('audio') == -1) {
          _self.$message({
            message: "请上传正确的音频格式",
            offset: 120,
            center: true
          });
          return false;
        } else {
          return new Promise((resolve, reject) => {
            policy().then(response => {
              console.log(response)
              _self.dataObj.policy = response.data.policy;
              _self.dataObj.signature = response.data.signature;
              _self.dataObj.ossaccessKeyId = response.data.accessKeyId;
              _self.dataObj.dir = response.data.dir;
              _self.dataObj.key = response.data.dir + '/' + _self.uuid + "${filename}";
              _self.dataObj.host = response.data.host;
              _self.action = response.data.action;
              resolve(true)
            }).catch(err => {
              console.log(err)
              reject(false)
            })
          })
        }
      },
      handleUploadSuccess(res, file) {
        this.audioFlag = false;
        this.audioUploadPercent = 0;
        this.showFileList = true;
        this.fileList.pop();
        this.fileList.push({
          name: file.name,
          url: this.dataObj.host + '/' + this.dataObj.dir + '/' + this.uuid +file.name
        });
        this.emitInput(this.fileList[0].url);
      },
      handleClose() {
        this.dialogVisible = false;
        this.stop()
      },
      stop() {
        var audio = document.querySelector('#audio');
        audio.pause();
      },
      uploadAudioProcess(event, file, fileList) {
        this.audioFlag = true;
        this.audioUploadPercent = parseInt(file.percentage.toFixed(0));
      },
    }
  }
</script>
<style scoped>
  /deep/ .el-upload {
    text-align: left;
  }
</style>


